package com.xuanzheng.company.servlet;

import com.google.gson.Gson;
import com.xuanzheng.company.entity.SystemApplication;
import com.xuanzheng.company.entity.User;
import com.xuanzheng.company.service.SystemApplicationService;
import com.xuanzheng.company.service.impl.SystemApplicationServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@WebServlet("/getApplications")
public class GetApplicationsServlet extends HttpServlet {
    private final SystemApplicationService applicationService = new SystemApplicationServiceImpl();
    private final Gson gson = new Gson();

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        response.setContentType("application/json;charset=UTF-8");
        PrintWriter out = response.getWriter();
        
        Map<String, Object> result = new HashMap<>();
        
        // 检查用户会话
        HttpSession session = request.getSession(false);
        if (session == null || session.getAttribute("user") == null) {
            result.put("success", false);
            result.put("message", "请先登录");
            out.write(gson.toJson(result));
            return;
        }
        
        // 获取当前用户
        User user = (User) session.getAttribute("user");
        
        // 检查用户角色，必须是系统管理员
        if (user.getRole() != 2) {
            result.put("success", false);
            result.put("message", "只有系统管理员可以查看系统使用申请列表");
            out.write(gson.toJson(result));
            return;
        }
        
        // 获取查询参数
        String statusStr = request.getParameter("status");
        String keyword = request.getParameter("keyword");
        
        List<SystemApplication> applications;
        
        // 根据参数决定查询方式
        if (statusStr != null && !statusStr.trim().isEmpty()) {
            try {
                int status = Integer.parseInt(statusStr);
                
                if (keyword != null && !keyword.trim().isEmpty()) {
                    // 状态+关键词搜索
                    applications = applicationService.searchApplicationsWithStatus(status, keyword);
                } else {
                    // 仅按状态查询
                    applications = applicationService.getApplicationsByStatus(status);
                }
            } catch (NumberFormatException e) {
                result.put("success", false);
                result.put("message", "状态值无效");
                out.write(gson.toJson(result));
                return;
            }
        } else if (keyword != null && !keyword.trim().isEmpty()) {
            // 仅关键词搜索
            applications = applicationService.searchApplications(keyword);
        } else {
            // 查询所有
            applications = applicationService.getAllApplications();
        }
        
        result.put("success", true);
        result.put("applications", applications);
        out.write(gson.toJson(result));
    }
} 